<template>
  <div>
    <el-drawer
      @close="handleClose"
      destroy-on-close
      title="菜单授权"
      :visible="show"
      direction="rtl"
      ref="drawer"
      close-on-press-escape
      size="30%"
    >
      <div>
        <div class="j-drawer__content">
          <el-tree
            v-loading="treeLoading"
            :data="treeDatas"
            show-checkbox
            default-expand-all
            :check-strictly="true"
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps"
          >
            <span class="j-drawer__content__tree" slot-scope="{ node, data }">
              <span>{{ node.label }}</span>
              <span>
                <el-tag
                  size="mini"
                  :type="data.status === true ? 'success' : 'danger'"
                  disable-transitions
                  >{{ data.status === true ? "启用" : "禁用" }}</el-tag
                >
                <el-tag
                  size="mini"
                  :type="data.type === false ? 'primary' : 'info'"
                  disable-transitions
                  >{{ data.type === false ? "菜单" : "按钮" }}</el-tag
                >
              </span>
            </span>
          </el-tree>
        </div>
        <div class="j-drawer__footer">
          <el-button type="primary" size="small" @click="clickSubmitForm"
            >确定</el-button
          >
          <el-button size="small" @click="handleClose">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listTreeMenus } from "@/api/system/menu";
import { listRoleMenus, insertBatchRoleMenus } from "@/api/system/role-menu";
export default {
  name: "MenuAuthPicker",
  props: {
    show: {
      type: Boolean,
      required: true,
    },
    data: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      treeDatas: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      treeLoading: false,
    };
  },
  created() {
    this.loadTree();
  },
  methods: {
    // 查询树
    loadTree() {
      const t = this;
      t.treeLoading = true;
      setTimeout(() => {
        listTreeMenus(1)
          .then((response) => {
            t.treeLoading = false;
            t.treeDatas = response.data;

            t.loadRoleMenus();
          })
          .catch(() => {
            t.treeLoading = false;
          });
      }, 1000);
    },
    // 查询角色菜单
    loadRoleMenus() {
      const t = this;
      listRoleMenus({ roleId: t.data })
        .then((response) => {
          const nodes = response.data.map((m) => {
            return m.menuId;
          });
          t.$refs.tree.setCheckedKeys(nodes);
        })
        .catch(() => {});
    },
    // 保存
    clickSubmitForm() {
      const t = this;
      const datas = [];
      const rows = t.$refs.tree
        .getCheckedKeys()
        .concat(t.$refs.tree.getHalfCheckedKeys());

      rows.forEach((element) => {
        datas.push({
          roleId: t.data,
          menuId: element,
        });
      });

      insertBatchRoleMenus({ id: t.data }, datas)
        .then((response) => {
          t.$notify({
            title: "成功",
            message: response.message,
            type: "success",
            position: "bottom-left",
            duration: 2000,
          });

          t.handleClose();
        })
        .catch(() => {});
    },
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.j-drawer__content {
  padding: 0px 24px;
  margin-bottom: 56px;
  overflow: auto;

  .j-drawer__content__tree {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
}

.j-drawer__footer {
  display: flex;
  height: 56px;
  width: 100%;
  align-items: center;
  position: absolute;
  bottom: 0px;
  box-sizing: border-box;
  justify-content: flex-end;
  border-top: 1px solid #f0f0f0;
  padding-right: 24px;
  background-color: #fff;
  z-index: 999;
}
</style>
